package com.y.architecture.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.y.architecture.R;
import com.y.architecture.base.BaseActivity;
import com.zhengsr.viewpagerlib.indicator.TabIndicator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

public class TabActivity extends BaseActivity {

    @BindView(R.id.line_indicator)
    TabIndicator tabIndecator;

    @BindView(R.id.viewPager)
    ViewPager viewPager;


    List<String> mTitle = Arrays.asList("新闻","娱乐","学习",
            "新闻","娱乐","学习",
            "新闻","娱乐","学习");
    private List<Fragment> mFragments = new ArrayList<>();
    @Override
    public int getContentView() {
        return R.layout.activity_tab;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
//        for (String string : mTitle) {
//            CusFragment fragment = CusFragment.newInStance(string);
//            mFragments.add(fragment);
//        }
    }
    @Override
    public void initViews(Bundle savedInstanceState) {
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));
        /**
         * 把 TabIndicator 跟viewpager关联起来
         */
        // 设置 viewpager的切换速度，这样点击的时候比较自然
        tabIndecator.setViewPagerSwitchSpeed(viewPager,600);
        tabIndecator.setTabData(viewPager,mTitle, new TabIndicator.TabClickListener() {
            @Override
            public void onClick(int position) {
                //顶部点击的方法公布出来
                viewPager.setCurrentItem(position);
            }
        });
    }
    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {


        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return  mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}
